/*
 * @Description: 设置(App设置)    AppSetUp
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-30 16:25:12
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2020-01-04 09:17:43
 */
import 'package:C20Driver/provider/driver_information.dart';
import 'package:C20Driver/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:C20Driver/resources/app_storage_interface.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/mqtt_manage.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/service/socket_manage.dart';
import 'package:C20Driver/utils/app_update.dart';
// import 'package:C20Driver/utils/jpush.dart';
import 'package:C20Driver/utils/locate_info.dart';
import 'package:C20Driver/widget/arrow_list.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppSetUp extends StatefulWidget {
  @override
  _AppSetUpState createState() => _AppSetUpState();
}

class _AppSetUpState extends State<AppSetUp> {
  String versionNumber = '0.0.0';

  @override
  void initState() {
    super.initState();
    Utils.getVersionNumber().then((res) {
      setState(() {
        versionNumber = res;
      });
    });
  }

  /// 退出登录当前账号
  Future _openAlertDialog(context) async {
    final action = await showDialog(
        context: context,
        barrierDismissible: false, // 点击空白处是否可以关闭
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('你确定退出当前登录账号？'),
            actions: <Widget>[
              FlatButton(
                child: Text('取消'),
                onPressed: () {
                  Navigator.pop(context, 'cancel');
                },
              ),
              FlatButton(
                child: Text('确认'),
                onPressed: () {
                  LocateInfo().stopLocation(context);
                  Navigator.pop(context, 'ok');
                },
              ),
            ],
          );
        });

    switch (action) {
      case 'ok':

        cleanAppLoginState(context);
        break;
      case 'cancel':
        break;
      default:
    }
  }

  // 清除app登录状态，清除token，返回登录页
  cleanAppLoginState(context) async {
    SocketManage().stopSocket(context);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    MqttManage.getInstance().disconnect();
    Provider.of<DriverInformation>(context, listen: false).cleanDriverInfo();
    prefs.setString(AppStorageInterface.LOGINTOKEN, ''); // 存储token到本地
    NavigatorUtil.jump(context, '/login');
    // Jpush.getJpushInstance().cleanAlias();
    // Jpush.getJpushInstance().cleanAllTags();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的设置'),
        elevation: 0,
      ),
      body: Container(
        padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(32),
          right: ScreenUtil().setWidth(32),
        ),
        width: double.infinity,
        height: double.infinity,
        color: Color(0xffffffff),
        child: Column(
          children: <Widget>[
            SizedBox(
              height: ScreenUtil().setHeight(30.0),
            ),
            InkWell(
              child: Container(
                height: ScreenUtil().setWidth(130),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            '个人信息',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Color(0xff333333),
                            ),
                          ),
                          Text(
                            '',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Colors.black38,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right, color: Colors.black26),
                  ],
                ),
              ),
              onTap: () {
                // AppUpdate().getAppUpdate(context);
                NavigatorUtil.jump(context, '/personalData');
              },
            ),
            Divider(
              color: Color(0xffEEEEEE),
              height: ScreenUtil().setWidth(1),
            ),
            InkWell(
              child: Container(
                height: ScreenUtil().setWidth(130),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            '关于',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Color(0xff333333),
                            ),
                          ),
                          Text(
                            '',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Color(0xff333333),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right, color: Colors.black26),
                  ],
                ),
              ),
              onTap: () {
                NavigatorUtil.jump(context, '/about');
              },
            ),
            Divider(
              color: Color(0xffEEEEEE),
              height: ScreenUtil().setWidth(1),
            ),
            InkWell(
              child: Container(
                height: ScreenUtil().setWidth(130),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            '账号注销',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Color(0xff333333),
                            ),
                          ),
                          Text(
                            '',
                            style: TextStyle(
                              fontSize: ScreenUtil().setSp(32.0),
                              color: Colors.black38,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_right, color: Colors.black26),
                  ],
                ),
              ),
              onTap: () => Request.logOffUser(context),
            ),
            Divider(
              color: Color(0xffEEEEEE),
              height: ScreenUtil().setWidth(1),
            ),
            SizedBox(
              height: ScreenUtil().setHeight(57.0),
            ),
            Container(
              width: ScreenUtil().setWidth(686.0),
              height: ScreenUtil().setWidth(100.0),
              decoration: BoxDecoration(
                color: Color(0xffEEF6EE),
                borderRadius: BorderRadius.all(
                  Radius.circular(
                    ScreenUtil().setWidth(50),
                  ),
                ),
              ),
              child: MaterialButton(
                textColor: Color(0xff668D6E),
                child: Text(
                  '退出登录',
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(32),
                      fontWeight: FontWeight.w500),
                ),
                onPressed: () {
                  _openAlertDialog(context);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
